package com.fy.tt.service.impl;

import com.fy.tt.action.TemplateTool;
import com.fy.tt.dao.ParserDao;
import com.fy.tt.entity.Parser;
import com.fy.tt.entity.query.ParserQuery;
import com.fy.tt.entity.vo.ParserVo;
import com.fy.tt.service.ParserService;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@Slf4j
public class ParserServiceImpl implements ParserService {
    Connection dbConnection = null;
    ParserDao parserDao = null;

    {
        try {
            dbConnection = TemplateTool.getDBConnection();
            parserDao = TemplateTool.getBean(ParserDao.class);
        } catch (Exception e) {
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }

    @Override
    public List<Parser> getAllParsers() {
        List<Parser> parsers = null;
        try {
            parsers = parserDao.getAllParsers(dbConnection);
        } catch (Exception e) {

        }
        return parsers;
    }

    @Override
    public int addParser(Parser parser) {
        int i = -1;
        try {
            parserDao.save(dbConnection, parser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int deleteParser(int id) {
        int i = -1;
        try {
            i = parserDao.deleteParser(dbConnection, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateParser(Parser parser) {
        int i = -1;
        try {
            i = (int) parserDao.updateIgnoreNull(dbConnection, parser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<Parser> getPageParsers(int pageSize, int current) {
        List<Parser> parsers = null;
        try {
            parsers = parserDao.getPageParsers(dbConnection, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return parsers;
    }

    @Override
    public List<ParserVo> getConditionPageParsers(ParserQuery parserQuery, int pageSize, int current) {
        List<ParserVo> parsers = null;
        try {
            parsers = parserDao.getConditionPageParsers(dbConnection, parserQuery, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return parsers;
    }

    @Override
    public Parser getParserByObjId(Long bandObjId) {
        Parser parser = null;
        try {
            parser = parserDao.getParserByObjId(dbConnection, bandObjId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return parser;
    }
}
